package com.zxb.rocketmq.springboot_rocketmq_demo.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;


@Slf4j
@Component
@RocketMQTransactionListener
public class TransactionListenerImpl implements RocketMQLocalTransactionListener {
    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        log.info("executeLocalTransaction");
        // 执行本地事务
        String tag = String.valueOf(message.getHeaders().get("rocketmq_TAGS"));
        if ("TAGA".equals(tag)) {
            //这里只讲TAGA消息提交，状态为可执行
            return RocketMQLocalTransactionState.COMMIT;
        } else if ("TAGB".equals(tag)) {
            return RocketMQLocalTransactionState.ROLLBACK;
        } else if ("TAGC".equals(tag)) {
            return RocketMQLocalTransactionState.UNKNOWN;
        }
        return RocketMQLocalTransactionState.UNKNOWN;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        log.info("checkLocalTransaction===>{}", message);
        return RocketMQLocalTransactionState.COMMIT;
    }
}
